package com.hngy.xpq.android_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.hngy.xpq.android_practice.database.SQLiteUser;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.user_main.UserUpdateKey;

public class OrderActivity extends AppCompatActivity {

    private EditText name,key;
    private Button dl,zc,wj;
    private CheckBox bc;
    SQLiteUser sqLiteUser;
    SharedPreferences Main,notification;
    ImageButton order_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_dl_order);
        sqLiteUser = new SQLiteUser(this);
        UserControl();
        UserMethod();
        UserMian();
    }

    private void UserControl() {   //Android控件
        dl = findViewById(R.id.user_xpq_dl);
        zc = findViewById(R.id.user_xpq_zc);
        wj = findViewById(R.id.button_wj_key);
        name = findViewById(R.id.user_xpq_name);
        key = findViewById(R.id.user_xpq_key);
        bc = findViewById(R.id.box_bc);
        order_image = findViewById(R.id.order_image);
        Main = super.getSharedPreferences("main",MODE_PRIVATE);
        notification = super.getSharedPreferences("notification",MODE_PRIVATE);
    }

    private void UserMian() {     //保存的账号和密码的导出
        name.setText(notification.getString("name",""));
        if (notification.getString("check","").equals("1")){
            key.setText(notification.getString("key",""));
            bc.setChecked(true);
        }
    }

    private void UserMethod() {    //连接界面

        dl.setOnClickListener(new View.OnClickListener() {   //登陆，连接主界面
            @Override
            public void onClick(View v) {
                String index_name = name.getText().toString();
                String index_key = key.getText().toString();
                if (TextUtils.isEmpty(index_name)) {
                    IndexToast("请输入账号");
                } else if (TextUtils.isEmpty(index_key)) {
                    IndexToast("请输入密码");
                } else {
//                    ByteArrayOutputStream boas = new ByteArrayOutputStream();
//                    Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.jxdf)).getBitmap();
//                    bitmap.compress(Bitmap.CompressFormat.PNG , 100 , boas);
//                    byte[] a = boas.toByteArray();
//                    String d = new TcpImage().Transits(a);


                    String information = "DL001//" + index_name + "//" + index_key;
                    String a = new TcpClient().Transits(information);
                    Toast.makeText(OrderActivity.this, a ,Toast.LENGTH_SHORT).show();
                    if ( a.equals("登录成功") ){
                        //存储账号和密码
                        SharedPreferences.Editor editor = Main.edit();
                        editor.putString("name",index_name);
                        if (bc.isChecked()){
                            editor.putString("key",index_key);
                            editor.putString("check","1");
                        } else {
                            editor.putString("key","");
                            editor.putString("check","0");
                        }
                        editor.commit();
                        SharedPreferences.Editor editor1 = notification.edit();
                        editor1.putString("name",index_name);
                        if (bc.isChecked()){
                            editor1.putString("key",index_key);
                            editor1.putString("check","1");
                        } else {
                            editor1.putString("key","");
                            editor1.putString("check","0");
                        }
                        editor1.commit();
                        Intent intent = new Intent(OrderActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
        //注册密码
        zc.setOnClickListener(new View.OnClickListener() {    //注册，连接注册界面
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderActivity.this,OrderUserRegist.class);
                startActivity(intent);
            }
        });
        //忘记密码
        wj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderActivity.this, UserUpdateKey.class);
                startActivity(intent);
                finish();
            }
        });
        //商家界面
        order_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderActivity.this,MerchantActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    public void IndexToast(String text){
        Toast.makeText(OrderActivity.this,text,Toast.LENGTH_SHORT).show();
    }

}